app.use( express.json({ verify: (req, res, buf) => { req.rawBody = buf.toString(); }, }), ); app.post('/webhook', async (req, res, next) => { const sig = req.headers['stripe-signature']; let event; try { event = stripe.webhooks.constructEvent(req.rawBody, sig, endpoint_secret); } catch (err) { return console.log(err) } if (event.type === 'invoice.payment_succeeded') { //... } res.send(); }); Intenté seguir este enlace pero seguí obteniendo express.raw is not a function , también probé esto:
app.use((req, res, next) => { if (req.originalUrl === '/webhook') { next(); } else { express.json()(req, res, next); } });Y todavía tengo el mismo error, realmente agradecería si pudiera obtener ayuda.
Estoy usando Firebase Cloud Functions como mi API de webhook, así:
import webhook_app_creator from "express"; import cors from "cors"; // This example uses Express to receive webhooks //the Cloud Function calls the webhook_app export const webhook_app = webhook_app_creator(); // The Firebase Admin SDK to access Cloud Firestore. //const cors = require("cors"); // Automatically allow cross-origin requests webhook_app.use(cors({ origin: true })); // build multiple CRUD interfaces: webhook_app.post("/direct", async (request, response) => { //send the response early - the only valid response is "received" await commonHandler(request, response, endpointDirectSecret); response.json({ received: true }); }); webhook_app.post("/connect", async (request, response) => { //send the response early - the only valid response is "received" await commonHandler(request, response, endpointSecret); response.json({ received: true }); }); const commonHandler = async (request, response, secret) => { const sig = request.headers["stripe-signature"]; try { request.fullEvent = stripe.webhooks.constructEvent( request.rawBody, sig, secret ); } catch (err) { logger(`Webhook Error: ${err.message}`); return; } return queue_event(request.fullEvent); };La función de la nube no podría ser más sencilla:
//import functions from "firebase-functions"; import { functions, webhook_app } from "../../../../services"; // Expose Express API as a single Cloud Function: export default functions.https.onRequest(webhook_app);